<#include "license.ftl">
<@license/>
<#macro copy_arg br>
<#assign maxNumber = 0>
<#assign num = 0>
<#list br.params.param as param>
    <#assign num = num + 1>
    <#if param?contains(',')>
        <#assign index = param?index_of(',')>
        <#assign param = param?substring(0,index)>
    </#if>
    <#if param?number gte maxNumber>
        <#assign maxNumber = param?number>
        <#if num gt 1>,</#if>arguments.get(${param?number}).isObject().get("argument").toString()
    </#if>
</#list>
</#macro>
package ${doc.all.package}.gwt.constants;

import redora.client.validation.DefaultMessageAllocator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;

/**
* @author Redora (www.redora.net)
*/
public class MessageAllocator extends DefaultMessageAllocator {

    static Messages messages = GWT.create(Messages.class);

    @Override
    public String rule(String objectName, int ruleId, JSONArray arguments) {
        if (ruleId < 0) {
            return defaultRule(ruleId, arguments);
        }

<#list doc["/all/object"] as model>
    <#if model.businessRules[0]??>
        if (objectName.equals("${model.@name}")) {
            switch (ruleId) {
        <#list model.businessRules.businessRule as br>
            <#assign defaultMessage = "">
            <#list br.message as message>
                <#if message.@language == "en">
                    <#assign defaultMessage = message>
                </#if>
            </#list>
            case ${br.@number}:
                return messages.${model.@name?uncap_first}_businessRule_${br.@number}(<#if br.params[0]??><@copy_arg br=br/></#if>);	
        </#list>
            }
        }
    </#if>
</#list>
		return null;
    }

    @Override
    public String message(String objectName, int messageId, JSONArray arguments) {
<#list doc["/all/object"] as model>
    <#if model.messages[0]??>
        if (objectName.equals("${model.@name}")) {
            switch (messageId) {
        <#list model.messages.messageBundle as br>
            <#assign defaultMessage = "">
            <#list br.message as message>
                <#if message.@language == "en">
                    <#assign defaultMessage = message>
                </#if>
            </#list>
                case ${br.@number}:
                    return messages.${model.@name?uncap_first}_message_${br.@number}(<#if br.params[0]??><@copy_arg br=br/></#if>);
        </#list>
            }
        }
    </#if>
</#list>
        return null;
    }
}